package com.guass.nettyprotostuff.disruptor;

import com.guass.nettyprotostuff.rpc.common.ModelMsg;
import com.lmax.disruptor.RingBuffer;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import org.apache.zookeeper.server.admin.Command;

/**
 * @author guass
 * @date 2021年12月22日 17:41
 */
public class CommandProducer {

    private String producerId;

    private RingBuffer<TranslatorDataWapper> ringBuffer;

    private TranslatorDataWapper translatorDataWapper = new TranslatorDataWapper();

    public CommandProducer(String producerId, RingBuffer<TranslatorDataWapper> ringBuffer) {
        this.producerId = producerId;
        this.ringBuffer = ringBuffer;
    }

    public void onData(ChannelHandlerContext ctx, ModelMsg modelMsg) {
        long sequence = ringBuffer.next();
        try {
            translatorDataWapper = ringBuffer.get(sequence);
            translatorDataWapper.setCtx(ctx);
            translatorDataWapper.setModelMsg(modelMsg);
        } finally {
            ringBuffer.publish(sequence);
        }
    }
}
